package org.codefinger.json.parser.deserializer;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.codefinger.json.parser.JSONArrayDeserializer;
import org.codefinger.json.parser.JSONLexer;
import org.codefinger.json.parser.JSONObjectDeserializer;
import org.codefinger.json.parser.JSONParseException;

public class ArrayDeserializerForLongArray implements JSONArrayDeserializer {

	private ArrayList<Long>	arrayList	= new ArrayList<Long>();

	public ArrayDeserializerForLongArray() {
		super();
	}

	@Override
	public JSONArrayDeserializer createArrayDeserializer() {
		throw new JSONParseException("Can not parse this json to long[].");
	}

	@Override
	public JSONObjectDeserializer createObjectDeserializer() {
		throw new JSONParseException("Can not parse this json to long[].");
	}

	@Override
	public void addValue(Object value) {
		throw new JSONParseException("Can not parse this json to long[].");
	}

	@Override
	public void addStringValue(JSONLexer jsonLexer) {
		arrayList.add(Long.parseLong(jsonLexer.getOriginalString()));
	}

	@Override
	public void addIntValue(String value) {
		arrayList.add(Long.parseLong(value));
	}

	@Override
	public void addFloatValue(String value) {
		arrayList.add(new BigDecimal(value).longValue());
	}

	@Override
	public void addFalseValue() {
		arrayList.add(0L);
	}

	@Override
	public void addTrueValue() {
		arrayList.add(1L);
	}

	@Override
	public void addNullValue() {
		throw new JSONParseException("Can not parse this json to long[].");
	}

	
	@Override
	public Object getDeserializedValue() {
		int size = arrayList.size();
		long[] array = new long[size];
		for (int i = 0; i < size; i++) {
			array[i] = arrayList.get(i);
		}
		return  array;
	}

}
